//
//  JWAddressVC.m
//  jinwang
//
//  Created by jzb on 2018/3/9.
//  Copyright © 2018年 jzb. All rights reserved.
//

#import "JWAddressVC.h"
#import "JWAddressCell.h"
#import "JWAddNewAddressVC.h"
#import "JWAddressModel.h"

@interface JWAddressVC ()
{
    NSMutableArray *_AddressModelArr;
}
@property (nonatomic,strong) JWAddressCell *cell;

@end

@implementation JWAddressVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"地址";
    self.view.backgroundColor = [UIColor whiteColor];
    [self setRightItemWithImageNamed:@"addBtnImg"];//消息
    [self setUpTableView];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self net_getUserAddressList];
    
}
//配置tableView
-(void)setUpTableView{
//    self.tableView.delegate = self;
//    self.tableView.dataSource = self;
    self.tableView.estimatedRowHeight = 44;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    //注册cell
    [self registerCellWithCellClassArray:@[NSStringFromClass([JWAddressCell class])]];
    
    //下拉刷新
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self net_getUserAddressList];
    }];
}

#pragma mark -UITableViewDelegate,UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _AddressModelArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    self.cell = [tableView dequeueReusableCellWithIdentifier:[JWAddressCell getCellReuseIdentifier]];
    self.cell.contentView.backgroundColor = [UIColor clearColor];
    
//    [self.cell.GeRenZiLiaoBtn addTarget:self action:@selector(cellButtonOnclikc:) forControlEvents:UIControlEventTouchUpInside];
    
    self.cell.model = _AddressModelArr[indexPath.row];
    
    return self.cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 80;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if ([_isChose isEqualToString:@"000"]) {
        
    }else{
        
        JWAddNewAddressVC *vc = [[JWAddNewAddressVC alloc]init];
        vc.isEdit = @"YES";
        vc.model = _AddressModelArr[indexPath.row];
        [self.navigationController pushViewController:vc animated:YES];
    }
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView setEditing:NO animated:YES];
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"你确定删除该地址？" preferredStyle:UIAlertControllerStyleAlert];
        [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
        [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
            
            JWAddressModel *model = [JWAddressModel new];

            model = _AddressModelArr[indexPath.row];
            [[DDNetworkManagerDate makeUrl_delAddress_with_token:k_user_token address_id:[NSString stringWithFormat:@"%@",model.address_id]] post_RequestFinshSuccess:^(id responseObject) {
                [self.tableView.mj_header endRefreshing];

                if ([self requestFinshCode:responseObject isShowMessage:YES] == 1) {
                    [_AddressModelArr removeObjectAtIndex:indexPath.row];
                    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
                }

            } failure:^(id errorObject) {
                [self.tableView.mj_header endRefreshing];
                [self showBottomMessag:@"网络请求失败"];
            }];
            
        }]];
        
        [self presentViewController:alertController animated:YES completion:nil];
    }
    
}
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";//默认文字为 Delete
}

//先要设Cell可编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return YES;
}
//定义编辑样式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return UITableViewCellEditingStyleDelete;
}
-(void)rightItemACTION{
    BOOL isLogin = [[ToolManager sharedManager] checkLoginStateAtVC:self];
    if (!isLogin) {
        return;
    }
    JWAddNewAddressVC *vc = [[JWAddNewAddressVC alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark -网络请求
//获取所有的收货地址
-(void)net_getUserAddressList{
    [[DDNetworkManagerDate makeUrl_getUserAddressList_with_token:k_user_token] post_RequestFinshSuccess:^(id responseObject) {
        [self.tableView.mj_header endRefreshing];
        if ([self requestFinshCode:responseObject isShowMessage:NO] == 1) {
            NSArray *data = [responseObject objectForKey:kRequsetDataKey];
            _AddressModelArr = [NSMutableArray array];
            [data enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                JWAddressModel *model = [JWAddressModel yy_modelWithDictionary:obj];
                [_AddressModelArr addObject:model];
            }];
        }

        //刷新
        [self.tableView reloadData];
    } failure:^(id errorObject) {
        [self.tableView.mj_header endRefreshing];
        [self showBottomMessag:@"网络请求失败"];
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
